//
//  NetWorkMonitor.m
//  DemoProject
//
//  Created by liu xiwang on 2018/5/8.
//  Copyright © 2018年 Sunqk. All rights reserved.
//

#import "NetWorkMonitor.h"
#import "AFNetworkReachabilityManager.h"
#import "UserManager.h"
#define KLoadDataBase        @"LoadDataWithNetWorkChanged"
@implementation NetWorkMonitor

+ (instancetype)sharedNetWorkMonitor{
    static NetWorkMonitor *sharedNetWorkMonitor = nil;
    static dispatch_once_t predicate;
    dispatch_once(&predicate, ^{
        sharedNetWorkMonitor = [[NetWorkMonitor alloc] init];
        [UserManager sharedUserManager].appRunInfo.hasNet = YES;
    });
    return sharedNetWorkMonitor;
}

- (void)statrMonitorNetworkState{
    AFNetworkReachabilityManager *manager = [AFNetworkReachabilityManager sharedManager];
    [manager startMonitoring];
    [manager setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
        switch (status) {
            case AFNetworkReachabilityStatusNotReachable:
                NSLog(@"没有网络");
                [UserManager sharedUserManager].appRunInfo.hasNet = NO;
                [[NSNotificationCenter defaultCenter] postNotification:[NSNotification notificationWithName:KLoadDataBase object:nil userInfo:@{@"netType":@"NotReachable"}]];
                
                break;
            case AFNetworkReachabilityStatusUnknown:
                NSLog(@"未知");
                if(! [UserManager sharedUserManager].appRunInfo.hasNet){
                    [[NSNotificationCenter defaultCenter] postNotification:[NSNotification notificationWithName:KLoadDataBase object:nil userInfo:@{@"netType":@"Unknown"}]];
                }
                [UserManager sharedUserManager].appRunInfo.hasNet = YES;
                break;
            case AFNetworkReachabilityStatusReachableViaWiFi:
                NSLog(@"WiFi");
                if(![UserManager sharedUserManager].appRunInfo.hasNet){
                    [[NSNotificationCenter defaultCenter] postNotification:[NSNotification notificationWithName:KLoadDataBase object:nil userInfo:@{@"netType":@"WiFi"}]];
                }
                [UserManager sharedUserManager].appRunInfo.hasNet = YES;
                break;
            case AFNetworkReachabilityStatusReachableViaWWAN:
                NSLog(@"3G|4G");
                if(![UserManager sharedUserManager].appRunInfo.hasNet){
                    [[NSNotificationCenter defaultCenter] postNotification:[NSNotification notificationWithName:KLoadDataBase object:nil userInfo:@{@"netType":@"WWAN"}]];
                }
                [UserManager sharedUserManager].appRunInfo.hasNet = YES;
                break;
            default:
                break;
        }
    }];
}

@end
